package com.xzm.project.annotation;

import com.xzm.project.ui.bean.Car;

import java.lang.reflect.Field;

/**
 * author : xuzongmeng
 * date   : 2019/9/2
 * desc   : 描述
 */
public class AnnotationCar {
    private static AnnotationCar annotationCar;
    public static AnnotationCar instance(){
        synchronized (AnnotationCar.class){
            if (annotationCar==null){
                annotationCar = new AnnotationCar();
            }
            return annotationCar;
        }
    }

    public void inject(Object object){
        Class<?> aClass = object.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            if (field.getName().equals("car") && field.isAnnotationPresent(MyTag.class)){
                MyTag annotation = field.getAnnotation(MyTag.class);
                Class<?> type = field.getType();
                if (Car.class.equals(type)){
                    try {
                        field.setAccessible(true);
                        field.set(object,new Car(annotation.name(),annotation.size()));
                    }catch (IllegalAccessException  e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
